const app = getApp();
Page({
  data: {
    //控制评论输入框
    openpl: false,
    //回复评论输入框
    openpl2: false,
    //获取用户输入
    userpl: null,
    userpl2: null,
    //用户输入字符数
    userplLength: 0,
    userpl2Length: 0,
    //评论数量
    sum: 0,
    //评论数据
    plarr: []
  },
  //选项卡开关
  xxk1: false,
  xxk2: false,
  //对话框开关
  modal1: false,
  modal2: false,
  //接收点击的数据父级的index
  jfIndex: null,
  //接收点击的数据子级的index
  jzIndex: null,
  //接收点击的数据的父级
  jParent: null,
  //接收点击数据的点赞状态
  jZan: null,
  //接收点击的数据的用户id
  jUserid: null,
  //接收点击的数据的用户昵称
  jName: null,
  //加载用户
  onLoad: function () {
    this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
    });
    this.setData({
      nickName: this.data.logininfo.userInfo.nickName,
      getopenid: this.data.openid
    })
    console.log(this.data.nickName)
    console.log(this.data.openid);
    console.log(this.data.logininfo);
    var arr = this.data.plarr
    var count = 0;
    if (arr.length != 0) {
      for (var i = 0; i <= arr.length - 1; i++) {
        var a = arr[i].plhf.length
        count += a
      }
    }
    count += arr.length
    this.setData({
      sum: count
    })
    console.log(this.data.sum)
  },
  //开启输入框
  opensrk: function () {
    this.setData({
      openpl: true,
      openpl2: false
    })
  },
  //失去焦点
  usershuru: function (e) {
    var val = e.detail.value.replace(/\s+/g, '')
    this.setData({
      openpl: false,
      openpl2: false,
    })
  },
  //获取用户输入字符数量
  shuruing: function (e) {
    var val = e.detail.value.replace(/\s+/g, '')
    var vallength = val.length
    this.setData({
      userpl: val,
      userplLength: vallength
    })
  },
  shuruing2: function (e) {
    var val = e.detail.value.replace(/\s+/g, '')
    var vallength = val.length
    this.setData({
      userpl2: val,
      userpl2Length: vallength
    })
  },
  //点击发布
  issuepl: function () {
    var nr = this.data.userpl
    console.log(nr)
    var arr = this.data.plarr
    // var pName = arr[pIndex].nickname
    var pName = this.data.nickName
    if (arr.length >= 1) {
      var last = arr.length - 1
      var olbid = arr[last].id
      var olbflor = arr[last].floor
      var newID = parseInt(olbid) + 1
      var newflor = parseInt(olbflor) + 1
    } else {
      var newID = 1
      var newflor = 1
    }
    arr.push({ "id": newID, "userid": "111", "tx": "", "nickname": pName, "author": true, "floor": newflor, "time": "刚刚", "content": nr, "dianzan": false, "zansum": 0, "plhf": [] })
    this.setData({
      plarr: arr
    })
    var arr = this.data.plarr
    var count = 0;
    if (arr.length != 0) {
      for (var i = 0; i <= arr.length - 1; i++) {
        var a = arr[i].plhf.length
        count += a
      }
    }
    count += arr.length
    this.setData({
      sum: count
    })
    console.log(this.data.plarr)
    wx: wx.showToast({
      title: '提交中',
      icon: 'loading',
      duration: 1000,
    })
    this.setData({
      userpl: null,
      userplLength: 0,
      jfIndex: null,
      jzIndex: null,
      jParent: null,
      jUserid: null,
      jZan: null,
      openpl: false,
      openpl2: false
    })
  },
  issuepl2: function () {
    var dValue = this.data.userpl2
    var pIndex = this.data.jfIndex
    var arr = this.data.plarr
    var parentId = arr[pIndex].id
    // var pName = arr[pIndex].nickname
    var pName = this.data.nickName
    var last = arr.length - 1
    var olbid = arr[last].id
    var newID = parseInt(olbid) + 1
    arr[pIndex].plhf.push({ "id": newID, "userid": "111", "parent": parentId, "hfuser": pName, "tx": "", "nickname": pName, "author": true, "time": "刚刚", "content": dValue, "dianzan": false, "zansum": 0 })
    this.setData({
      plarr: arr
    })
    var arr = this.data.plarr
    var count = 0;
    if (arr.length != 0) {
      for (var i = 0; i <= arr.length - 1; i++) {
        var a = arr[i].plhf.length
        count += a
      }
    }
    count += arr.length
    this.setData({
      sum: count
    })
    wx: wx.showToast({
      title: '提交中',
      icon: 'loading',
      duration: 1000,
    })
    this.setData({
      userpl2: null,
      userpl2Length: 0,
      jfIndex: null,
      jzIndex: null,
      jParent: null,
      jUserid: null,
      jZan: null,
      openpl: false,
      openpl2: false
    })
  },
  //点赞
  dianzan: function (e) {
    var dIndex = e.currentTarget.dataset.index
    var arr = this.data.plarr
    var zan = arr[dIndex].zansum
    if (arr[dIndex].dianzan) {
      arr[dIndex].dianzan = false
      arr[dIndex].zansum = zan - 1
      this.setData({
        plarr: arr
      })
    } else {
      arr[dIndex].dianzan = true
      arr[dIndex].zansum = parseInt(zan) + 1
      this.setData({
        plarr: arr
      })
    }
  },
  //回复评论点赞
  subdianzan: function (e) {
    console.log(e)
    var eIndex = e.currentTarget.dataset.index
    console.log(eIndex)
    var eParent = e.currentTarget.dataset.parent
    console.log(eParent)
    var arr = this.data.plarr
    for (var i = 0; i <= arr.length - 1; i++) {
      if (arr[i].id == eParent) {
        var zan = arr[i].plhf[eIndex].zansum
        if (arr[i].plhf[eIndex].dianzan) {
          arr[i].plhf[eIndex].dianzan = false
          arr[i].plhf[eIndex].zansum = zan - 1
          this.setData({
            plarr: arr
          })
        } else {
          arr[i].plhf[eIndex].dianzan = true
          arr[i].plhf[eIndex].zansum = parseInt(zan) + 1
          this.setData({
            plarr: arr
          })
        }
      }
    }
  },
  //开启选项卡
  openxxk1: function (e) {
    console.log(e)
    var vIndex = e.currentTarget.dataset.index
    var vUser = e.currentTarget.dataset.user
    var arr = this.data.plarr
    var vZan = arr[vIndex].dianzan
    this.setData({
      jfIndex: vIndex,
      jUserid: vUser,
      jZan: vZan,
      xxk1: true
    })
    console.log(this.data.jfIndex)
  },
  openxxk2: function (e) {
    var vIndex = e.currentTarget.dataset.index
    var vParent = e.currentTarget.dataset.parent
    var vUser = e.currentTarget.dataset.user
    var arr = this.data.plarr
    for (var i = 0; i <= arr.length - 1; i++) {
      if (arr[i].id == vParent) {
        var vZan = arr[i].plhf[vIndex].dianzan
        var vFindex = i
      }
    }
    this.setData({
      jfIndex: vFindex,
      jzIndex: vIndex,
      jParent: vParent,
      jUserid: vUser,
      jZan: vZan,
      xxk2: true
    })
    console.log(this.data.jfIndex)
    console.log(this.data.jzIndex)
    console.log(this.data.jParent)
  },
  //关闭选项卡
  closexxk: function () {
    this.setData({
      jfIndex: null,
      jzIndex: null,
      jParent: null,
      jUserid: null,
      jZan: null,
      xxk1: false,
      xxk2: false
    })
  },
  //评论回复
  actionhf: function () {
    // var name = arr[dIndex].nickname
    var name = this.data.nickName
    console.log(name)
    this.setData({
      jName: name,
      openpl: false,
      openpl2: true,
      xxk1: false,
      xxk2: false
    })
  },
  actionhf2: function () {
    // var name = arr[dIndex].nickname
    var name = this.data.nickName
    console.log(name)
    this.setData({
      jName: name,
      openpl: false,
      openpl2: true,
      xxk1: false,
      xxk2: false
    })
  },
  // 留言点赞
  actiondz: function () {
    var dIndex = this.data.jfIndex
    var arr = this.data.plarr
    var zan = arr[dIndex].zansum
    if (arr[dIndex].dianzan) {
      arr[dIndex].dianzan = false
      arr[dIndex].zansum = zan - 1
      this.setData({
        plarr: arr,
        jfIndex: null,
        jzIndex: null,
        jParent: null,
        jUserid: null,
        jZan: null,
        xxk1: false,
        xxk2: false
      })
    } else {
      arr[dIndex].dianzan = true
      arr[dIndex].zansum = parseInt(zan) + 1
      this.setData({
        plarr: arr,
        jfIndex: null,
        jzIndex: null,
        jParent: null,
        jUserid: null,
        jZan: null,
        xxk1: false,
        xxk2: false
      })
    }
  },
  actiondz2: function () {
    var dIndex = this.data.jzIndex
    var dParent = this.data.jParent
    var arr = this.data.plarr
    for (var i = 0; i <= arr.length - 1; i++) {
      if (arr[i].id == dParent) {
        var zan = arr[i].plhf[dIndex].zansum
        if (arr[i].plhf[dIndex].dianzan) {
          arr[i].plhf[dIndex].dianzan = false
          arr[i].plhf[dIndex].zansum = zan - 1
          this.setData({
            plarr: arr,
            jfIndex: null,
            jzIndex: null,
            jParent: null,
            jUserid: null,
            jZan: null,
            xxk1: false,
            xxk2: false
          })
        } else {
          arr[i].plhf[dIndex].dianzan = true
          arr[i].plhf[dIndex].zansum = parseInt(zan) + 1
          this.setData({
            plarr: arr,
            jfIndex: null,
            jzIndex: null,
            jParent: null,
            jUserid: null,
            jZan: null,
            xxk1: false,
            xxk2: false
          })
        }
      }
    }
  },
  //删除评论
  actionsc: function () {
    this.setData({
      xxk1: false,
      xxk2: false,
      modal1: true,
      modal2: false
    })
  },
  actionsc2: function () {
    this.setData({
      xxk1: false,
      xxk2: false,
      modal1: false,
      modal2: true
    })
  },
  //关闭对话框
  modalclose: function () {
    this.setData({
      jfIndex: null,
      jzIndex: null,
      jParent: null,
      jUserid: null,
      jZan: null,
      xxk1: false,
      xxk2: false,
      modal1: false,
      modal2: false,
    })
  },
  //确认对话框1
  modalok1: function () {
    var dIndex = this.data.jfIndex
    var arr = this.data.plarr
    arr.splice(dIndex, 1);
    this.setData({
      plarr: arr,
      jfIndex: null,
      jzIndex: null,
      jParent: null,
      jUserid: null,
      jZan: null,
      xxk1: false,
      xxk2: false,
      modal1: false,
      modal2: false,
    })
    wx: wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 1500,
    })
    var arr = this.data.plarr
    var count = 0;
    if (arr.length != 0) {
      for (var i = 0; i <= arr.length - 1; i++) {
        var a = arr[i].plhf.length
        count += a
      }
    }
    count += arr.length
    this.setData({
      sum: count
    })
  },
  modalok2: function () {
    var dIndex = this.data.jzIndex
    var dParent = this.data.jParent
    var arr = this.data.plarr
    for (var i = 0; i <= arr.length - 1; i++) {
      if (arr[i].id == dParent) {
        arr[i].plhf.splice(dIndex, 1);
      }
      this.setData({
        plarr: arr,
        jfIndex: null,
        jzIndex: null,
        jParent: null,
        jUserid: null,
        jZan: null,
        xxk1: false,
        xxk2: false,
        modal1: false,
        modal2: false,
      })
    }
    wx: wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 1500,
    })
    var arr = this.data.plarr
    var count = 0;
    if (arr.length != 0) {
      for (var i = 0; i <= arr.length - 1; i++) {
        var a = arr[i].plhf.length
        count += a
      }
    }
    count += arr.length
    this.setData({
      sum: count
    })
  }
})